package com.myboot.demo.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.myboot.demo.dto.OrderDTO;
import com.myboot.demo.entity.OrderDetail;
import com.myboot.demo.entity.OrderMaster;
import com.myboot.demo.enums.ResultEnum;
import com.myboot.demo.exception.SellException;
import com.myboot.demo.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 17:42 2018/12/22
 */
@Slf4j
public class OrderForm2OrderMasterConverter {

    public static OrderDTO converter(OrderForm orderForm){
        Gson gson =new Gson();
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        orderDTO.setBuyerAddress(orderForm.getAddress());

        List<OrderDetail> orderDetailList =new ArrayList<>();
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(),
                new TypeToken<List<OrderDetail>>(){
                }.getType());
        }
        catch (Exception e){
            log.error("【对象转换】错误 string ={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }
}
